(() => {
  class Queue {
    constructor($indexedDB, $exception) {
      this.indexedDB = $indexedDB;
      this.exception = $exception;
    }
    post (followers) {
      return new Promise((resolve, reject) => {
        this.indexedDB.openStore('queue', (store) => {
          store.insert(followers).then((result) => {
            resolve(result);
          }, (error) => {
            this.exception.handle(error);
            reject(error);
          });
        });
      });
    }
    delete (openid) {
      return new Promise((resolve, reject) => {
        this.indexedDB.openStore('queue', (store) => {
          if (openid) {
            store.delete(openid).then((result) => {
              resolve(result);
            }, (error) => {
              this.exception.handle(error);
              reject(error);
            });
          }
          else {
            store.clear().then((result) => {
              resolve(result);
            }, (error) => {
              this.exception.handle(error);
              reject(error);
            });
          }
        });
      });
    }
    put (followers) {
      return new Promise((resolve, reject) => {
        this.indexedDB.openStore('queue', (store) => {
          store.upsert(followers).then((result) => {
            resolve(result);
          }, (error) => {
            this.exception.handle(error);
            reject(error);
          });
        });
      });
    }
    get (openid) {
      return new Promise((resolve, reject) => {
        this.indexedDB.openStore('queue', (store) => {
          if (openid) {
            store.find(openid).then((result) => {
              resolve(result);
            }, (error) => {
              this.exception.handle(error);
              reject(error);
            });
          }
          else {
            store.getAll().then((result) => {  
              resolve(result);
            },
            (error) => {
              reject(error);
            });
          }
        });
      });
    }
  }
  angular.module('app').factory('$queue', ['$indexedDB', '$exception', ($indexedDB, $exception) => new Queue($indexedDB, $exception)]);
})()